<?php
// 后台用户模块
class OrderShipAction extends CommonAction {
	public function _initialize() {
		parent::_initialize();
		Vendor('Alidayu.TopSdk');
	}

	function _filter(&$map) {
	}
	
	public function index() {
		//列表过滤器，生成查询Map对象
		// $map = $this->_search();
		// if (method_exists($this, '_filter')) {
			// $this->_filter($map);
		// }
		
		if(!empty($_REQUEST['dispatch_datetime'])){
			$name  = $this->getActionName();
			$model = D($name);
			$voList = $model->query("SELECT os.*,ac.city_name,ad.region_name,oi.collect_datetime,oi.dispatch_datetime 
			FROM ar_shop.order_ship AS os
			LEFT JOIN ar_shop.order_info AS oi ON oi.order_id = os.order_id
			LEFT JOIN ar_shop.payment_info AS pi ON pi.order_id = os.order_id
			LEFT JOIN ar_shop.address_city AS ac ON ac.city_id = os.city
			LEFT JOIN ar_shop.address_district AS ad ON ad.region_id = os.district
			WHERE pi.payment_status = 10 AND oi.order_status <> 20 AND left(oi.dispatch_datetime,10) = '".$_REQUEST['dispatch_datetime']."'");
			$orders = "";
			foreach($voList as $vo){
				if($orders==""){
					$orders = "'".$vo['order_id']."'";
				}else{
					$orders .= ",'".$vo['order_id']."'";
				}
			}
			$db_res = $model->query("SELECT od.order_id,od.commodity_id,od.order_num,ci.`name`,ci.spec,ci.unit_id FROM ar_shop.order_detail AS od
			LEFT JOIN ar_shop.commodity_info AS ci ON ci.commodity_id = od.commodity_id
			WHERE od.order_id in (".$orders.")");
			$order_commodity = "";
			foreach($db_res as $db_one){
				$od['commodity_id'] = $db_one['commodity_id'];
				$od['order_num'] = $db_one['order_num'];
				$od['name'] = $db_one['name'];
				$od['spec'] = $db_one['spec'];
				$od['unit_name'] = C('GOODS_UNIT_NAME.'.$db_one['unit_id']);
				$order_commodity[$db_one['order_id']][] = $od;
			}
			$this->assign('order_commodity', $order_commodity);
		}
		$this->assign('list', $voList);
		$this->display();
		return;
	}
	
	protected function _search($name = '') {
		//生成查询条件
		$name  = $this->getActionName();
		$model = D($name);
		$map   = array();
		foreach ($model->getDbFields() as $key => $val) {
			if (isset($_REQUEST[$val]) && $_REQUEST[$val] != '') {
				$map[$val] = $_REQUEST[$val];
			}
		}
		return $map;
	}
	
	function _before_index(){
		$ac = D('AddressCity');
		$ad = D('AddressDistrict');
		$db_res = $ac->select();
		$city_kv = array();
		foreach($db_res as $db_one){
			$city_kv[$db_one['city_id']] = $db_one['city_name'];
		}
		$db_res = $ad->select();
		$district_kv = array();
		foreach($db_res as $db_one){
			$district_kv[$db_one['region_id']] = $db_one['region_name'];
		}
		$this->assign('city_kv',$city_kv);
		$this->assign('district_kv',$district_kv);
	}
	
	function setship(){
		$name  = $this->getActionName();
		$model = D($name);
		$vo = $model->where("ship_id = '".$_REQUEST['id']."'")->find();
		if(!empty($_REQUEST['isset']) && $_REQUEST['isset']==1){//set操作
			if(empty($_REQUEST['tracking_com'])) $this->error('请选择快递公司');
			if(!empty($vo['ship_status']) && $vo['ship_status']>=2){
				$this->error('该状态下无法修改');
			}
			
			$model->where("ship_id = '".$_REQUEST['id']."'")->save(array('tracking_com'=>$_REQUEST['tracking_com'],'tracking_nu'=>$_REQUEST['tracking_nu']));
			
			//更改订单状态
			$orderInfo = D('OrderInfo');
			$orderInfo->where("order_id = '".$vo['order_id']."'")->save(array('order_status'=>2));
			$this->success('设置成功'); 
		}
		
		
		$this->assign('vo',$vo);
		$this->display();
		return;
	}
	
	function shipmsg(){
		$name  = $this->getActionName();
		$model = D($name);
		$vo = $model->where("ship_id = '".$_REQUEST['id']."'")->find();
		
		if(!empty($_REQUEST['issend']) && $_REQUEST['issend']==1){//set操作
			if(!empty($vo['ship_status']) && $vo['ship_status']>=2){
				$this->error('该状态下无法发送通知');
			}
			if(empty($vo['tracking_com']) || empty($vo['tracking_nu'])){
				$this->error('快递信息不完整');
			}
		
			$c = new \TopClient;
			$c->appkey = C('ALIDAYU_CONFIG.app_key');
			$c->secretKey = C('ALIDAYU_CONFIG.app_secret');
			$req = new \AlibabaAliqinFcSmsNumSendRequest;
			$req->setSmsType("normal");
			$req->setSmsFreeSignName("食市");
			$req->setSmsParam("{\"order\":\"".$vo['order_id']."\",\"com\":\"".C('SHIP_COM_CODE.'.$vo['tracking_com'])."\",\"nu\":\"".$vo['tracking_nu']."\"}");
			$req->setRecNum($vo['mobile']);
			$req->setSmsTemplateCode("SMS_4905686");
			$c->execute($req);
			
			$model->where("ship_id = '".$_REQUEST['id']."'")->save(array('ship_status'=>2));
			$this->success('发送成功'); 
		}
		
		
		$msg = "尊敬的客户，您的订单（".$vo['order_id']."），已出库发货，正在配送中。快递公司：".C('SHIP_COM_CODE.'.$vo['tracking_com'])."，快递单号：".$vo['tracking_nu']."。";
		$this->assign('msg',$msg);
		$this->assign('vo',$vo);
		$this->display();
		return;
	}
	
	//用户签收
	function usersign(){
		$name  = $this->getActionName();
		$model = D($name);
		$vo = $model->where("ship_id = '".$_REQUEST['id']."'")->find();
		if(empty($vo['tracking_com']) || empty($vo['tracking_nu'])){
			$this->error('快递信息不完整');
		}
		if($vo['ship_status']==10){
			$this->error('快递已经签收');
		}
		
		$model->where("ship_id = '".$_REQUEST['id']."'")->save(array('ship_status'=>10));
		//更改订单状态
		$orderInfo = D('OrderInfo');
		$orderInfo->where("order_id = '".$vo['order_id']."'")->save(array('order_status'=>10));
		$this->success('操作成功'); 
	}
	
	function Export(){
		if(empty($_REQUEST['dispatch_datetime'])){
			echo "请选择配送时间";exit;
		}
		$ac = D('AddressCity');
		$ad = D('AddressDistrict');
		$db_res = $ac->select();
		$city_kv = array();
		foreach($db_res as $db_one){
			$city_kv[$db_one['city_id']] = $db_one['city_name'];
		}
		$db_res = $ad->select();
		$district_kv = array();
		foreach($db_res as $db_one){
			$district_kv[$db_one['region_id']] = $db_one['region_name'];
		}
		$this->assign('city_kv',$city_kv);
		$this->assign('district_kv',$district_kv);
		
		$order_commodity = array();
		$voList = array();
		if(!empty($_REQUEST['dispatch_datetime'])){
			$name  = $this->getActionName();
			$model = D($name);
			$voList = $model->query("SELECT os.*,ac.city_name,ad.region_name,oi.collect_datetime,oi.dispatch_datetime 
			FROM ar_shop.order_ship AS os
			LEFT JOIN ar_shop.order_info AS oi ON oi.order_id = os.order_id
			LEFT JOIN ar_shop.payment_info AS pi ON pi.order_id = os.order_id
			LEFT JOIN ar_shop.address_city AS ac ON ac.city_id = os.city
			LEFT JOIN ar_shop.address_district AS ad ON ad.region_id = os.district
			WHERE pi.payment_status = 10 AND oi.order_status <> 20 AND left(oi.dispatch_datetime,10) = '".$_REQUEST['dispatch_datetime']."'");
			$orders = "";
			foreach($voList as $vo){
				if($orders==""){
					$orders = "'".$vo['order_id']."'";
				}else{
					$orders .= ",'".$vo['order_id']."'";
				}
			}
			$db_res = $model->query("SELECT od.order_id,od.commodity_id,od.order_num,ci.`name`,ci.spec,ci.unit_id FROM ar_shop.order_detail AS od
			LEFT JOIN ar_shop.commodity_info AS ci ON ci.commodity_id = od.commodity_id
			WHERE od.order_id in (".$orders.")");
			$order_commodity = "";
			foreach($db_res as $db_one){
				$od['commodity_id'] = $db_one['commodity_id'];
				$od['order_num'] = $db_one['order_num'];
				$od['name'] = $db_one['name'];
				$od['spec'] = $db_one['spec'];
				$od['unit_name'] = C('GOODS_UNIT_NAME.'.$db_one['unit_id']);
				$order_commodity[$db_one['order_id']][] = $od;
			}
			
		}
		
		
		$filename = 'shishiDispatchReport_'.date('Y-m-d');
		header("Pragma:public");
		header("Expires:0");
		header("Cache-Control:must-revalidate,post-check=0,pre-check=0");
		header("Content-Type:application/force-download");
		header("Content-Type:application/vnd.ms-execl;charset=gb2312");
		header("Content-Type:application/octet-stream");
		header("Content-Type:application/download");
		header('Content-Disposition:attachment;filename="' . $filename . '.xls"');
		header("Content-Transfer-Encoding:binary");
		
		
		$this->assign('order_commodity', $order_commodity);
		$this->assign('list', $voList);
        $this->display();
	}
}
?>